{% form_theme form '::fields2tab.html.twig' %}
<div class="contentwrapper">
    <div class="heading">
        <h3>Nouveau Employe</h3>
    </div>
    <div class="row-fluid">
        <div class="span12">
            <form id="form_employer" action="" {{ form_enctype(form) }} method="POST" class="form-horizontal seperator">
                    {{ form_row(form.username) }}
                    {{ form_row(form.email) }}
                    {{ form_row(form.plainPassword) }}
                    {{ form_row(form.nom) }}
                    {{ form_row(form.prenom) }}
                    {{form_row(form.cin)}}
                    {{form_row(form.telephone)}}
                    {{form_row(form.function)}}
                    {{ form_row(form.photo) }}
                    {% if is_granted('ROLE_SUPER_ADMIN') %}
                    <div class="form-row form-2-tab">
                        <div class="span12">
                            <div class="row-fluid">
                                <label class="form-label span3 required" for="fos_user_registration_form_function">
                                    Est Admin :

                                </label>
                                <input type="checkbox" id="is_admin"  />
                            </div>
                        </div>
                    </div>
                    {{ form_row(form.departement) }}
                    {% else %}
                    <input type="hidden" id="fos_user_registration_form_departement" name="fos_user_registration_form[departement]" value="{{app.user.departement.Id}}" /> 
                    {% endif %}

                    {{ form_row(form._token) }}
                    <div id="form-permission" style="width: 100%" class="form-row form-2-tab">
                        <div class="span12">
                            <label style="width: 11%" class="form-label span3 required" for="fos_user_registration_form_username">
                                Permission :
                            </label>
                            <div class="span12">
                                <div class="span3">
                                    <div class="box closed">

                                        <div class="title">

                                            <h4>
                                                <span class="icon16 brocco-icon-grid"></span>
                                                <span>Permission Achat</span>
                                            </h4>
                                            <a href="#" class="minimize">Minimize</a>
                                        </div>
                                        <div class="content">
                                            <div class="todo">
                                                <h4>
                                                    Fournisseur
                                                    <span style="float: right" class="controls">
                                                        <a class="tip" href="#" oldtitle="Edit task" title="" data-hasqtip="true">
                                                            <button  data-role="#role_fournissuer" style="min-width: 27px;" class="btn btn-success btn-mini btn-add-tout-role" type="submit"><span style=" font-size: 20px;font-weight: bold;">+</span></button>
                                                        </a>
                                                        <a class="tip" href="#" oldtitle="Remove task" title="" data-hasqtip="true">
                                                            <button  data-role="#role_fournissuer" style="min-width: 27px;display: none" class="btn btn-danger btn-mini btn-delete-tout-role" type="submit"><span style=" font-size: 20px;font-weight: bold;">-</span></button>
                                                        </a>
                                                    </span>
                                                </h4>
                                                <ul id="role_fournissuer">
                                                    <li class="clearfix"><div class="txt">
                                                            ajouter fournissuer
                                                        </div>
                                                        <div class="controls">
                                                            <a class="tip" href="#" oldtitle="Edit task" title="" data-hasqtip="true">
                                                                <button data-role="ROLE_FOURNISSEUR_NEW"  style="min-width: 27px;" class="btn btn-success btn-mini btn-add-role" type="submit"><span style=" font-size: 20px;font-weight: bold;">+</span></button>
                                                            </a>
                                                            <a class="tip" href="#" oldtitle="Remove task" title="" data-hasqtip="true">
                                                                <button data-role="ROLE_FOURNISSEUR_NEW"  style="min-width: 27px;display: none" class="btn btn-danger btn-mini btn-delete-role" type="submit"><span style=" font-size: 20px;font-weight: bold;">-</span></button>
                                                            </a>
                                                        </div>
                                                    </li>
                                                    <li class="clearfix"><div class="txt">
                                                            afficher information fournissuer
                                                        </div>
                                                        <div class="controls">
                                                            <a class="tip" href="#" oldtitle="Edit task" title="" data-hasqtip="true">
                                                                <button data-role="ROLE_FOURNISSEUR_SHOW"  style="min-width: 27px;" class="btn btn-success btn-mini btn-add-role" type="submit"><span style=" font-size: 20px;font-weight: bold;">+</span></button>
                                                            </a>
                                                            <a class="tip" href="#" oldtitle="Remove task" title="" data-hasqtip="true">
                                                                <button data-role="ROLE_FOURNISSEUR_SHOW"  style="min-width: 27px;display: none" class="btn btn-danger btn-mini btn-delete-role" type="submit"><span style=" font-size: 20px;font-weight: bold;">-</span></button>
                                                            </a>
                                                        </div>
                                                    </li>
                                                    <li class="clearfix"><div class="txt">
                                                            modifier information fournissuer
                                                        </div>
                                                        <div class="controls">
                                                            <a class="tip" href="#" oldtitle="Edit task" title="" data-hasqtip="true">
                                                                <button data-role="ROLE_FOURNISSEUR_EDIT"  style="min-width: 27px;" class="btn btn-success btn-mini btn-add-role" type="submit"><span style=" font-size: 20px;font-weight: bold;">+</span></button>
                                                            </a>
                                                            <a class="tip" href="#" oldtitle="Remove task" title="" data-hasqtip="true">
                                                                <button data-role="ROLE_FOURNISSEUR_EDIT"   style="min-width: 27px;display: none" class="btn btn-danger btn-mini btn-delete-role" type="submit"><span style=" font-size: 20px;font-weight: bold;">-</span></button>
                                                            </a>
                                                        </div>
                                                    </li>
                                                </ul>
                                            </div>

                                        </div>

                                    </div>
                                </div>

                                <div class="span3">
                                    <div class="box closed">

                                        <div class="title">

                                            <h4>
                                                <span class="icon16 brocco-icon-grid"></span>
                                                <span>Permission Vente</span>
                                            </h4>
                                            <a href="#" class="minimize">Minimize</a>
                                        </div>
                                        <div class="content">
                                            <div class="todo">
                                                <h4>
                                                    Client
                                                </h4>
                                                <ul id="role_client">
                                                    <li class="clearfix"><div class="txt">
                                                            Ajouter/Modifier client
                                                        </div>
                                                        <div class="controls">
                                                            <a class="tip" href="#" oldtitle="Edit task" title="" data-hasqtip="true">
                                                                <button data-role="ROLE_CLIENT_CRUD"  style="min-width: 27px;" class="btn btn-success btn-mini btn-add-role" type="submit"><span style=" font-size: 20px;font-weight: bold;">+</span></button>
                                                            </a>
                                                            <a class="tip" href="#" oldtitle="Remove task" title="" data-hasqtip="true">
                                                                <button data-role="ROLE_CLIENT_CRUD"  style="min-width: 27px;display: none" class="btn btn-danger btn-mini btn-delete-role" type="submit"><span style=" font-size: 20px;font-weight: bold;">-</span></button>
                                                            </a>
                                                        </div>
                                                    </li>
                                                    <li class="clearfix"><div class="txt">
                                                            Supprimer client
                                                        </div>
                                                        <div class="controls">
                                                            <a class="tip" href="#" oldtitle="Edit task" title="" data-hasqtip="true">
                                                                <button data-role="ROLE_CLIENT_DELETE"  style="min-width: 27px;" class="btn btn-success btn-mini btn-add-role" type="submit"><span style=" font-size: 20px;font-weight: bold;">+</span></button>
                                                            </a>
                                                            <a class="tip" href="#" oldtitle="Remove task" title="" data-hasqtip="true">
                                                                <button data-role="ROLE_CLIENT_DELETE"  style="min-width: 27px;display: none" class="btn btn-danger btn-mini btn-delete-role" type="submit"><span style=" font-size: 20px;font-weight: bold;">-</span></button>
                                                            </a>
                                                        </div>
                                                    </li>

                                                </ul>
                                            </div>

                                        </div>

                                    </div>
                                </div>

                            </div>
                        </div>


                    </div>
                    <div style="float: left;width: 90%" class="form-actions">
                        <button onclick="saveEmployer('n')" class="btn btn-info" type="button">Enregistrer et Nouveau</button>
                        <a href="{{path('gds_user_list')}}"><button class="btn" type="button">Retour vers liste</button></a>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <script>
                            $('#is_admin').removeAttr('checked')
                            var roles = new Array()
                            $('#fos_user_registration_form_plainPassword_second').attr("equalto", "#fos_user_registration_form_plainPassword_first")
                            $('#form_employer').validate({
                            })
                            $('.btn-add-tout-role').click(function() {
                                $($(this).attr('data-role') + ' .btn-add-role').click()
                            })
                            $('.btn-add-role').click(function() {
                                roles[roles.length] = $(this).attr('data-role');
                                $(this).css('display', 'none')
                                $(this).parent('a').parent('div').children('a:last').children('button').show()
                            })
                            $('.btn-delete-role').click(function() {
                                var index = roles.indexOf($(this).attr('data-role'));
                                roles.splice(index, 1);
                                $(this).css('display', 'none')
                                $(this).parent('a').parent('div').children('a:first').children('button').show()
                            })
                            $('#is_admin').change(function() {
                                alert($(this).attr('checked'))
                                if ($(this).attr('checked') == 'checked') {
                                    roles = new Array()
                                    roles[roles.length] = 'ROLE_ADMIN';
                                    $('.btn-delete-role').hide();
                                    $('.btn-add-role').show();
                                    $('#form-permission').hide()
                                } else {
                                    roles = new Array()
                                    $('#form-permission').show()
                                }
                            })
                            function saveEmployer(red) {
                                var select = $('<select multiple style="display: none" id="fos_user_registration_form_roles" name="fos_user_registration_form[roles][]"><option value="ROLE_USER" selected="selected"></option></select>')
                                $('#form_employer').append(select)
                                for (var i = 0; i < roles.length; i++) {
                                    select.append('<option value="' + roles[i] + '" selected="selected"></option>')
                                }
                                var data = $('#form_employer').serialize()
                                if ($('#form_employer').valid()) {
                                    $.ajax({
                                        type: 'post',
                                        dataType: 'json',
                                        url: '{{ path('fos_user_registration_register') }}',
                                        data: data,
                                        success: function(data) {
                                            if (data.success) {
                                                $.msgBox({
                                                    title: "Employer",
                                                    content: "Enregistrement Effectué avec succés",
                                                    type: "info",
                                                    buttons: [{value: "OK"}],
                                                    success: function(result) {
                                                        location.reload();
                                                    }
                                                });
                                            } else {
                                                $.msgBox({
                                                    title: "Employer",
                                                    content: "Erreur D'enregistrement",
                                                    type: "error",
                                                    buttons: [{value: "OK"}]
                                                });
                                            }
                                        }
                                    })
                                }
                            }
        </script>

